package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.HomeBuilderDrawingAuditMapper;
import com.eastfair.venueservice.dto.HomeBuilderDrawingAuditDTO;
import com.eastfair.venueservice.entity.HomeBuilderDrawingAudit;
import com.eastfair.venueservice.enumeration.HomeBuilderDrawingCategoryAuditDrawingSourceEnum;
import com.eastfair.venueservice.service.ExhibitionSpaceDrawingService;
import com.eastfair.venueservice.service.HomeBuilderDrawingAuditService;
import com.eastfair.venueservice.vo.HomeBuilderDrawingAuditVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 主场搭建商图纸审核记录
 * </p>
 *
 * @author dqq
 * @date 2023-04-28
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class HomeBuilderDrawingAuditServiceImpl extends SuperServiceImpl<HomeBuilderDrawingAuditMapper, HomeBuilderDrawingAudit> implements HomeBuilderDrawingAuditService {

    @Resource
    private ExhibitionSpaceDrawingService exhibitionSpaceDrawingService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<HomeBuilderDrawingAudit> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(HomeBuilderDrawingAudit model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<HomeBuilderDrawingAuditVO> listDrawingVOByCategoryId(Long categoryId) {
        log.info("listDrawingVOByCategoryId - 图纸分类审核ID查询图纸列表, categoryId={}", categoryId);
        HomeBuilderDrawingAuditDTO query = new HomeBuilderDrawingAuditDTO();
        query.setCategoryAuditId(categoryId);
        List<HomeBuilderDrawingAudit> list = listHomeBuilderDrawingAudit(query);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        return list.stream()
                .map(homeBuilderDrawingAudit -> BeanUtil.toBean(homeBuilderDrawingAudit, HomeBuilderDrawingAuditVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<HomeBuilderDrawingAudit> listHomeBuilderDrawingAudit(HomeBuilderDrawingAuditDTO query) {
        QueryWrapper<HomeBuilderDrawingAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(HomeBuilderDrawingAudit::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(query.getCategoryAuditId() != null, HomeBuilderDrawingAudit::getCategoryAuditId, query.getCategoryAuditId())
                .eq(query.getReportId() != null, HomeBuilderDrawingAudit::getReportId, query.getReportId())
                .eq(query.getDrawingSource() != null, HomeBuilderDrawingAudit::getDrawingSource, query.getDrawingSource())
                .in(query.getCategoryAuditIds() != null && !query.getCategoryAuditIds().isEmpty(), HomeBuilderDrawingAudit::getCategoryAuditId, query.getCategoryAuditIds())
                .in(query.getDrawingIdList() != null && !query.getDrawingIdList().isEmpty(), HomeBuilderDrawingAudit::getDrawingId, query.getDrawingIdList())
        ;
        return list(queryWrapper);
    }

    @Override
    public List<HomeBuilderDrawingAudit> listByReportIdAndSource(Long reportId, HomeBuilderDrawingCategoryAuditDrawingSourceEnum drawingSource) {
        HomeBuilderDrawingAuditDTO query = new HomeBuilderDrawingAuditDTO();
        query.setReportId(reportId);
        query.setDrawingSource(drawingSource.getCode());
        return listHomeBuilderDrawingAudit(query);
    }

    @Override
    public List<HomeBuilderDrawingAudit> listByCategoryAuditIds(List<Long> categoryAuditIds) {
        HomeBuilderDrawingAuditDTO query = new HomeBuilderDrawingAuditDTO();
        query.setCategoryAuditIds(categoryAuditIds);
        return listHomeBuilderDrawingAudit(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveHomeBuilderDrawingAudit(HomeBuilderDrawingAuditDTO homeBuilderDrawingAuditDTO) {
        HomeBuilderDrawingAudit homeBuilderDrawingAudit = BeanUtil.toBean(homeBuilderDrawingAuditDTO, HomeBuilderDrawingAudit.class);
        return save(homeBuilderDrawingAudit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<HomeBuilderDrawingAuditDTO> list) {
        if (list == null || list.isEmpty()) {
            return false;
        }
        list.forEach(this::saveHomeBuilderDrawingAudit);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<HomeBuilderDrawingAuditDTO> list, Long categoryAuditId) {
        if (list == null || list.isEmpty()) {
            return false;
        }
        list.forEach(homeBuilderDrawingAuditDTO -> {
            homeBuilderDrawingAuditDTO.setCategoryAuditId(categoryAuditId);
            saveHomeBuilderDrawingAudit(homeBuilderDrawingAuditDTO);
        });
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByReportIdAndType(Long reportId, HomeBuilderDrawingCategoryAuditDrawingSourceEnum drawingSource) {
        List<HomeBuilderDrawingAudit> list = listByReportIdAndSource(reportId, drawingSource);
        if (list == null || list.isEmpty()) {
            return false;
        }
        // 删除图纸
        removeByIdsOfLogic(list);
        // 删除展会展厅图纸
        List<String> imageUrls = list
                .stream()
                .map(HomeBuilderDrawingAudit::getImageUrl)
                .collect(Collectors.toList());
        exhibitionSpaceDrawingService.removeByImageUrl(imageUrls);
        return true;
    }

    @Override
    public boolean removeByReportIdAndDrawingId(Long reportId, List<Long> drawingIdList) {
        HomeBuilderDrawingAuditDTO query = new HomeBuilderDrawingAuditDTO();
        query.setReportId(reportId);
        query.setDrawingIdList(drawingIdList);
        List<HomeBuilderDrawingAudit> list = listHomeBuilderDrawingAudit(query);
        if (list == null || list.isEmpty()) {
            return false;
        }
        return removeByIdsOfLogic(list);
    }
}
